package com.lenovo.agricultureclient.ui.activity;

import java.util.ArrayList;

import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.ui.adapter.StudyImagePagerAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 用户引导界面，主要展示几张图片
 * @author zongbingwu
 *
 */
public class StudyActivity extends AppBaseActivity implements OnPageChangeListener
{
	//图片view容器
	private ViewPager mPager;
	//图片view适配器
	private StudyImagePagerAdapter mChartPagerAdp;
	//当前图片位置标记点
	private LinearLayout mTipsGroupView;
	//图片列表
	private ArrayList<String> mPicPathList;
	private boolean mFirstShow = true;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.study_activity);
    	
    	//初始化图片列表
    	mPicPathList = new ArrayList<String>();
    	mPicPathList.add("assets://main.png");
    	mPicPathList.add("assets://history.png");
    	mPicPathList.add("assets://control.png");
    	
		initView();
		//开始不断的获取传感器数据
		startGetSensorData();
    }
    
	private void initView()
	{
		mPager = (ViewPager)findViewById(R.id.image_pager);
		mTipsGroupView = (LinearLayout)findViewById(R.id.viewGroup);
		setupTipsImage();
		
		//实例化图片适配器
		mChartPagerAdp = new StudyImagePagerAdapter(this, mPicPathList, mImageLoader);
		//配置图片view容器
		mPager.setAdapter(mChartPagerAdp);
		mPager.setOnPageChangeListener(this);
		mPager.setCurrentItem(0);
		//图片点击事件处理函数
		mChartPagerAdp.setItemClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String path = (String)v.getTag();
				//只有点击最后一张图片才起作用
				if(path.equals(mPicPathList.get(2)))
				{
					//设置下次启动时不再显示用户引导界面
					mApp.setFirstRun(mFirstShow);
					//关闭自己
					//Intent intent = new Intent(StudyActivity.this, UserLoginActivity.class);
					//startActivity(intent);
					finish();
				}
			}
		});
		mChartPagerAdp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				mFirstShow = !isChecked;
			}
		});
	}
	
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		setTipImageSelect(arg0);
	} 
	
	//构建图片位置标记点
	private void setupTipsImage()
    {
    	mTipsGroupView.removeAllViews();
	    for(int i=0; i<mPicPathList.size(); i++)
	    {  
	        ImageView imageView = new ImageView(this);  
	        if(i == 0){  
	        	imageView.setBackgroundResource(R.drawable.page_indicator_focused);  
	        }else{  
	        	imageView.setBackgroundResource(R.drawable.page_indicator_unfocused);  
	        }
	        imageView.setLayoutParams(new LayoutParams(5,5)); 
	          
	        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(30, 30));  
	        layoutParams.leftMargin = 5;  
	        layoutParams.rightMargin = 5;  
	        mTipsGroupView.addView(imageView, layoutParams);  
	    }
    }
	
	//根据当前的图片位置来高亮相应的标记点
	private void setTipImageSelect(int selectItems){
        for(int i=0; i<mTipsGroupView.getChildCount(); i++)
        {  
        	View childView = mTipsGroupView.getChildAt(i);
            if(i == selectItems){  
            	childView.setBackgroundResource(R.drawable.page_indicator_focused);  
            }else{  
            	childView.setBackgroundResource(R.drawable.page_indicator_unfocused);  
            }  
        }  
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if(keyCode == KeyEvent.KEYCODE_BACK){
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
